<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script lang="javascript"> 
  // 封装ajax请求
    function ajax(options){
      const xhr = new XMLHttpRequest()

      options = options||{}
      options.type = (options.type||'GET').toUpperCase()
      options.dataType = options.dataType||'json'
      const params =options.data

      if(options.type==='GET'){
        xhr.open('GET',options.url+'?'+params,true)
        xhr.send(null)
      }else if(options.type==='POST'){
        xhr.open('POST',options.url,true)
        xhr.send(params)
      }
      xhr.onreadystatechange= function(){{
        if(xhr.readyState===4){
          let status = xhr.status
          if(status>=200&&status<300){
            options.success&&options.success(xhr.responseText,xhr.responseXML)
          }else{
            options.fail&&options.fail(status)
          }
        }
      }}
    }

    ajax({
      type:'post',
      dataType:'json',
      data:{},
      url:'https://xxxx',
      success:function(text,xml){
        console.log(text);
      },
      fail:function(status){
        console.log(status);
      }
    })
  </script>
</body>
</html>